﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RMS.Utils.MVP;
using RMS.Web.Shell.View;
using RMS.DataAccess.Entities;
using RMS.DataAccess.Repositories;
using RMS.Utils.Authentication;
using RMS.DataAccess.Common;
namespace RMS.Web.Shell.Presenter
{
    public class WriteReportPresenter : Presenter<IWriteReportView>
    {
        DoctorRequestsDtoRepository _doctorRequestsDtoRepository;
        IAuthentication _authentication;
        RadiologyImageRepository _radiologyImageRepository;
        RadiologyReportRepository _radiologyReportRepository;

        public WriteReportPresenter(DoctorRequestsDtoRepository doctorRequestsDtoRepository, IAuthentication authentication,
            RadiologyImageRepository radiologyImageRepository, RadiologyReportRepository radiologyReportRepository)
        {
            _doctorRequestsDtoRepository = doctorRequestsDtoRepository;
            _authentication = authentication;
            _radiologyImageRepository = radiologyImageRepository;
            _radiologyReportRepository = radiologyReportRepository;
        }


        public DoctorRequestsDto CurrentRequest(int RequestID)
        {
            return _doctorRequestsDtoRepository.Find(item =>
            {
                item.Employee_id = _authentication.CurrentLogedInMember.Id;
                item.RequestId = RequestID;
            }).Single();
        }
        public string[] GetImages(int requestID)
        {
            List<string> pathes = new List<string>();
            EntityResult<RadiologyImage> images = _radiologyImageRepository.Find(item => item.RequestId = requestID);
            foreach (RadiologyImage img in images.Rows)
            {
                pathes.Add(img.ImagePath.ToVirtualPath());
            }
            return pathes.ToArray();
        }
        public RadiologyReport getReport(int requestID)
        {
            try
            {
                return _radiologyReportRepository.Find(item => item.RequestID = requestID).Single();
            }
            catch (Exception)
            { 
            return new RadiologyReport();
            }
        }
        public EntityResult<DoctorRequestsDto> GetRequest(int patientId)
        {
            return _doctorRequestsDtoRepository.Find(item => item.PatientID = patientId);
        }
        public EntityResult<DoctorRequestsDto> GetRequest(String patientName)
        {
            string[] names = patientName.Split(' ');
            return _doctorRequestsDtoRepository.Find(items =>
            {
                items.Employee_id = _authentication.CurrentLogedInMember.Id;
                items.FirstName = names[0];
                if (names.Count() > 1)
                    items.FamilyName = names[1];
            });
        }
        public void InsertReport(RadiologyReport radiologyReport)
        {
            _radiologyReportRepository.Insert(radiologyReport);
        }
    }
}
